﻿# region Using Directives

using System;
using PMSytem.Windows;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;

using System.Windows.Threading;
using System.Threading;
using System.Windows.Input;
using PMSytem.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Shapes;
using PMSytem.Windows.App;

# endregion

namespace PMSytem.Windows.Tests
{
	public partial class MainPage : Page
	{
		#region Constructor

		public MainPage()
		{
			InitializeComponent();
			ToolWindow.ModalContainerPanel = this._ModalContainer;
			//this.btnHelp_Click(null, null);
            DataContext = this;
			CommandBinding cmdMaximizeAllWindows = new CommandBinding(
													   ToolWindowDockPanel.MaximizeAllWindowsCommand,
													   new ExecutedRoutedEventHandler(this.MaximizeAllWindow_Executed),
													   new CanExecuteRoutedEventHandler(this.CanExecuteMaximizeCommand));
			this.CommandBindings.Add(cmdMaximizeAllWindows);

			CommandBinding cmdRestoreAllWindows = new CommandBinding(
													  ToolWindowDockPanel.RestoreAllWindowsCommand,
													  new ExecutedRoutedEventHandler(this.RestoreAllWindow_Executed),
													  new CanExecuteRoutedEventHandler(this.CanExecuteRestoreCommand));
			this.CommandBindings.Add(cmdRestoreAllWindows);
		} 

		#endregion

		#region MaximizeAllWindow_Executed

		private void MaximizeAllWindow_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.content.MaximizeAllWindows();
		} 

		#endregion

		#region RestoreAllWindow_Executed

		private void RestoreAllWindow_Executed(object sender, ExecutedRoutedEventArgs e)
		{
			this.content.RestoreAllWindows();
		} 

		#endregion

		#region CanExecuteMaximizeCommand

		private void CanExecuteMaximizeCommand(object sender, CanExecuteRoutedEventArgs e)
		{
			foreach (ToolWindow window in this.content.OpenedWindows)
			{
				if (window.IsMaximized == false)
				{
					e.CanExecute = true;
					return;
				}
			}

			e.CanExecute = false;
		} 

		#endregion

		#region CanExecuteRestoreCommand

		private void CanExecuteRestoreCommand(object sender, CanExecuteRoutedEventArgs e)
		{
			foreach (ToolWindow window in this.content.OpenedWindows)
			{
				if (window.IsMaximized == true)
				{
					e.CanExecute = true;
					return;
				}
			}

			e.CanExecute = false;
		} 

		#endregion

		#region WindowsSubmenu_Opened

		private void WindowsSubmenu_Opened(object sender, RoutedEventArgs e)
		{
			this._WrapPanel.Children.Clear();

			foreach (ToolWindow window in this.content.OpenedWindows)
			{
				ReflectedButton btn = new ReflectedButton();

				Image img = new Image();
				img.VerticalAlignment = VerticalAlignment.Top;
				img.Margin = new Thickness(2);
				img.Width = 64;

				BitmapImage iconImage = new BitmapImage();
				iconImage.BeginInit();
				iconImage.UriSource = new Uri("Resources/Icons/128x128/Window128.png", UriKind.Relative);
				iconImage.DecodePixelWidth = 64;
				iconImage.EndInit();

				img.Source = iconImage;

				btn.Content = img;
				btn.Margin = new Thickness(5);
				btn.Text = window.Header.ToString();
				btn.Tag = window;

				Border b = new Border();
				b.Width = 100;
				b.Height = 100;
				b.Background = Brushes.AliceBlue;

				ToolTip t = new ToolTip();

				LinearGradientBrush toolTipBackground = new LinearGradientBrush();
				toolTipBackground.StartPoint = new Point(0, 0);
				toolTipBackground.EndPoint = new Point(0, 1);
				toolTipBackground.GradientStops.Add(new GradientStop(OfficeColors.Background.OfficeColor4, 0));
				toolTipBackground.GradientStops.Add(new GradientStop(OfficeColors.Background.OfficeColor5, 0.5));
				toolTipBackground.GradientStops.Add(new GradientStop(OfficeColors.Background.OfficeColor4, 1));

				t.Background = Brushes.White;
				t.BorderThickness = new Thickness(3);
				t.BorderBrush = toolTipBackground;
				t.Content = new Rectangle();
				t.Padding = new Thickness(4);
				
				btn.ToolTip = t;
				btn.ToolTipOpening += new ToolTipEventHandler(btn_ToolTipOpening);
				this._WrapPanel.Children.Add(btn);
                
			}
		}

		void btn_ToolTipOpening(object sender, ToolTipEventArgs e)
		{
			ReflectedButton btn = sender as ReflectedButton;
			ToolWindow window = btn.Tag as ToolWindow;
			ToolTip toolTip = btn.ToolTip as ToolTip;
			Rectangle rect = toolTip.Content as Rectangle;
			
			VisualBrush vb = new VisualBrush(window);
			vb.Opacity = 0.8;
			vb.AlignmentX = AlignmentX.Center;
			vb.AlignmentY = AlignmentY.Center;
			vb.Stretch = Stretch.Uniform;
			rect.Fill = vb;
			rect.Width = 250;
			rect.Height = window.ActualHeight * (250 / window.ActualWidth);
		} 

		#endregion

		#region btnHelp_Click

		void btnHelp_Click(object sender, RoutedEventArgs e)
		{
            //HelpWindow window = new HelpWindow();
            //window.Parent = this.content.WindowsContainer as Panel;
            //window.StartPosition = ToolWindowStartPosition.CenterParent;
            //window.CanResize = false;
            //window.ShowStatusBar = false;
            //window.Show();
		} 

		#endregion

		#region OnClick3

		private void OnClick3(object sender, RoutedEventArgs e)
		{
            //TestWindow window = new TestWindow();
            //window.Parent = this.content.WindowsContainer as Panel;
            //window.Show();
		} 

		#endregion

		#region Maximize / Restore

		private void Maximize_Click(object sender, RoutedEventArgs e)
		{
			this.content.MaximizeAllWindows();
		}

		private void Restore_Click(object sender, RoutedEventArgs e)
		{
			this.content.RestoreAllWindows();
		} 

		#endregion

		#region WindowsSubmenu_ButtonClick

		private void WindowsSubmenu_ButtonClick(object sender, RoutedEventArgs e)
		{
			ReflectedButton btn = e.Source as ReflectedButton;

			this.content.ShowWindow(btn.Tag as ToolWindow);

			if (sender is DropDownMenuItem)
			{
				((DropDownMenuItem)sender).CloseMenuItems();
			}
		} 

		#endregion

		private void btnRecord_Click(object sender, RoutedEventArgs e)
		{

			RecordWindow window = new RecordWindow();
			window.Parent = this.content.WindowsContainer as Panel;
			window.StartPosition = ToolWindowStartPosition.CenterParent;
			window.Show();
		}

		private void btnSearch_Click(object sender, RoutedEventArgs e)
		{
			SearchWindow window  = new SearchWindow();
			window.ToolWindowState = ToolWindowState.Maximized;
			window.StartPosition = ToolWindowStartPosition.CenterParent;
			window.Parent = this.content.WindowsContainer as Panel;
			window.Show();
		}

		private void btnEdit_Click(object sender, RoutedEventArgs e)
		{
            var currentuserid = LoginUser.CurrentUser.UserId;
			AddUser window = new AddUser(currentuserid);
            window.Header = "Edit Profile";
			window.ToolWindowState = ToolWindowState.Normal;
			window.StartPosition = ToolWindowStartPosition.CenterParent;
			window.Parent = this.content.WindowsContainer as Panel;
			window.Show();
		}

		private void btnAddUser_Click(object sender, RoutedEventArgs e)
		{
			AddUser window = new AddUser();
            window.Header = "Add User";
			window.ToolWindowState = ToolWindowState.Normal;
			window.StartPosition = ToolWindowStartPosition.CenterParent;
			window.Parent = this.content.WindowsContainer as Panel;
			window.Show();
		}

		private void btnSearchUser_Click(object sender, RoutedEventArgs e)
		{
			SearchUser window = new SearchUser();
            window.Header = "Search Users";
			window.ToolWindowState = ToolWindowState.Normal;
			window.StartPosition = ToolWindowStartPosition.CenterParent;
			window.Parent = this.content.WindowsContainer as Panel;
			window.Show();
		}

        private void btnAudit_Click(object sender, RoutedEventArgs e)
        {
            SearchAudit window = new SearchAudit();
            window.Header = "Audit Report";
            window.ToolWindowState = ToolWindowState.Normal;
            window.StartPosition = ToolWindowStartPosition.CenterParent;
            window.Parent = this.content.WindowsContainer as Panel;
            window.Show();
        }

        public bool HasPermissions_AddUser
        {
            get
            {
                if (LoginUser.IsAdminUser || LoginUser.IsOperatorUser)
                    return true;
                return false;
            }
        }

        public bool HasPermissions_SearchUser
        {
            get
            {
                if (LoginUser.IsAdminUser || LoginUser.IsOperatorUser)
                    return true;
                return false;
            }
        }

        public bool HasPermissions_AddRecord
        {
            get
            {
                if (LoginUser.IsAdminUser || LoginUser.IsOperatorUser)
                    return true;
                return false;
            }
        }
        public bool HasPermissions_Reports
        {
            get
            {
                if (LoginUser.IsAdminUser)
                    return true;
                return false;
            }
        }

        private void btnexit_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Do you really want to exit from the application?",
                         "Application exit Confirmation",
                          MessageBoxButton.YesNo,
                         MessageBoxImage.Question,
                          MessageBoxResult.OK,
                         new MessageBox.DialogResultEventHandler(this.DialogResult));
        }
        private void DialogResult(MessageBox.MessageBoxResultEventArgs e)
        {
            //this.LeftStatusContent = String.Format("Result of message: {0}", e.MessageResult.ToString());
            if (e.MessageResult.ToString() == "Yes")
            {
                 if(App.Current.Windows.Count>0)
                App.Current.Windows[0].Close();
            }
        }

        private void tablogout_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("Do you really want to Log out from the application?",
                        "Application Log out Confirmation",
                         MessageBoxButton.YesNo,
                        MessageBoxImage.Question,
                         MessageBoxResult.OK,
                        new MessageBox.DialogResultEventHandler(this.DialogResultLogout));
        }
        private void DialogResultLogout(MessageBox.MessageBoxResultEventArgs e)
        {
            //this.LeftStatusContent = String.Format("Result of message: {0}", e.MessageResult.ToString());
            if (e.MessageResult.ToString() == "Yes")
            {
                LoginUser.CurrentUser = null;
                if (App.Current.Windows.Count > 0)
                    App.Current.Windows[0].Close();
                //Window mainWindow = new Window();
                //mainWindow.Icon = BitmapFrame.Create(new Uri(@"pack://application:,,/Resources/Icons/16x16/Darf16.png"));
                //mainWindow.Width = 600;
                //mainWindow.Height = 300;
                //this.Parent.MainWindow = mainWindow;
                //mainWindow.Content = new Login(this.MainWindow, mainWindow);
                //mainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                //mainWindow.WindowState = WindowState.Normal;
                //mainWindow.WindowStyle = WindowStyle.ToolWindow;
                //mainWindow.ResizeMode = ResizeMode.NoResize;
                //mainWindow.Show();
            }
        }
        
	}
}